package com.toy.core.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 采用thread方法获得ApplicationContext,该方法spring2.5.5以上版本有
 * @author DELL
 *
 */
@Component
public class SpringContextHolder implements ApplicationContextAware{
	
	private static ApplicationContext applicationContext;
	
	public static Object getBean(String name){
		//return ContextLoader.getCurrentWebApplicationContext().getBean(name);
		return applicationContext.getBean(name);
	}
	
	public static <T> T getBean(Class<T> requiredType){
		//return ContextLoader.getCurrentWebApplicationContext().getBean(requiredType);
		return applicationContext.getBean(requiredType);
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringContextHolder.applicationContext = applicationContext;
	}
}
